
/*
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <stdio.h>

#include "libmooigraph.h"
#include "lmg.h"

static int span = 0;
static int startlevel = 0;
static void lmg_dfs2 (struct lmgnode *node);

void
lmg_dfs (void)
{
  struct lmgnodel *ptr;
  if (lmgnsingles)
    {
      startlevel = 1;
    }
  else
    {
      startlevel = 0;
    }
  if (lmgdebug)
    {
      printf ("%s(): start startlevel=%d\n", __FUNCTION__,startlevel);
    }
  ptr = lmgnodelist;
  while (ptr)
    {
      if (ptr->node->bitflags.done == 0)
	{
	  if (ptr->node->bitflags.hasin == 0
	      && ptr->node->bitflags.hasout == 0)
	    {
	      span = 0;
	    }
	  else
	    {
	      span = startlevel;
	    }
	  lmg_dfs2 (ptr->node);
	}
      ptr = ptr->next;
    }
  lmgnlevels = lmgmaxlevel + 1;
  if (lmgdebug)
    {
      printf ("%s(): start nlevels=%d\n", __FUNCTION__,lmgnlevels);
    }
  return;
}

static void
lmg_dfs2 (struct lmgnode *node)
{
  struct lmgedgel *eptr;
  if (node->bitflags.done)
    {
      return;
    }
  node->bitflags.done = 1;
  node->tree_layout.level = span;
  if (node->tree_layout.level > lmgmaxlevel)
    {
      lmgmaxlevel = node->tree_layout.level;
    }
  eptr = node->poe;
  while (eptr)
    {
      if (eptr->edge->from == node)
	{
	  if (eptr->edge->to->bitflags.done == 0)
	    {
	      span = span + 1;
	      lmg_dfs2 (eptr->edge->to);
	      span = node->tree_layout.level;
	    }
	}
      eptr = eptr->next;
    }
  return;
}

/* End */
